*****************************************************************
** do-file to create demand curves for WTP paper			   **
** last revised July 29 2020  by GEP				   **
*****************************************************************

clear
clear all
clear mata
clear matrix

set more off


set seed 710399 /// random.org min=100000, max=999999, Oct 28 2017 9:29pm

// set do file name
loc name_do "WTP Analysis File 2"
loc name_do_short "wtp_2"

// common tasks --- -include- rather than -run- or -do- to keep locals
// note that the pathname for code has to be hardcoded here but will be in locals after this initial call
qui include "code/setup-dofile.do"

// set log file name to be the name of the sub code .do file
local name_log "`name_do'" 

// ensure no log file of the same name is currently open
cap log close "${out_log}/`name_log'"

di "${out_log}/`name_log'"

log using "${out_log}/`name_log'", replace text name(`name_do_short')


timer on 100


// call analysis data set

use "${datain}wtp_analysis", clear

	*recoding/cleaning for R&R comments
	
	*recoding variables
	recode g1_01 g2_01 (2=0)
	label var g1_01 "Primary female owns a mobile phone"
	label var g2_01 "Primary male owns a mobile phone"
	*generate indicator for whether the respondent (male or female) owns a mobile
	gen resp_own_mobile = 0 if inlist(female,1) & inlist(g1_01,0) // female hh female doesn't own mobile
	replace resp_own_mobile = 0 if inlist(female,0) & inlist(g2_01,0) // male hh, male doesn't own mobile
	replace resp_own_mobile = 1 if inlist(female,1) & inlist(g1_01,1) // female hh, female owns mobile
	replace resp_own_mobile = 1 if inlist(female,0) & inlist(g2_01,1) // male hh, male owns mobile

	*analysis sample
    gen sample=wtp_consent==1 & wtp_second_stage==1 & wtp~=.

	keep if sample==1	
	
	// store data that retains female only households for producing figure D2
	tempfile all_strata
	save `all_strata', replace
*dropping single person strata
	drop if b1_09==2


	

	
***************************************************
// 1. matrices containing inverse demand information
****************************************************


matrix inv_demand_all = J(30,2,.)
matrix colnames inv_demand_all = "price" "share_buy"
matrix inv_demand_ag_nut = J(30,3,.)
matrix colnames inv_demand_ag_nut = "price" "share_buy_ag" "share_buy_nut"
matrix inv_demand_diff_ag_nut = J(30,4,.)
matrix colnames inv_demand_diff_ag_nut = "price" "diff" "ll" "ul"
matrix inv_demand_m_f = J(30,3,.)
matrix colnames inv_demand_m_f = "price" "share_buy_m" "share_buy_f"
matrix inv_demand_diff_m_f = J(30,4,.)
matrix colnames inv_demand_diff_m_f = "price" "diff" "ll" "ul"
matrix inv_demand_m_f_R = J(30,5,.)
matrix colnames inv_demand_m_f_R = "price" "dem_M_C" "dem_F_C" "dem_M_UW" "dem_F_UW"
matrix inv_demand_diff_m_f_byR = J(30,7,.)
matrix colnames inv_demand_diff_m_f_byR = "price" "diff_cent" "ll_cent" "ul_cent" "diff_uw" "ll_uw" "ul_uw"
matrix inv_demand_m_f_cell = J(30,5,.)
matrix colnames inv_demand_m_f_cell = "price" "dem_M_M" "dem_F_M" "dem_M_NM" "dem_F_NM"
matrix inv_demand_diff_m_f_cell = J(30,7,.)
matrix colnames inv_demand_diff_m_f_cell = "price" "diff_cell" "ll_cell" "ul_cell" "diff_ncell" "ll_ncell" "ul_ncell"
matrix inv_demand_ag_nut_R = J(30,5,.)
matrix colnames inv_demand_ag_nut_R = "price" "dem_A_C" "dem_N_C" "dem_A_UW" "dem_N_UW"
matrix inv_demand_diff_ag_nut_byR = J(30,7,.)
matrix colnames inv_demand_diff_ag_nut_byR = "price" "diff_cent" "ll_cent" "ul_cent" "diff_uw" "ll_uw" "ul_uw"
matrix inv_demand_diff_early = J(30,4,.)
matrix colnames inv_demand_diff_early = "price" "diff" "ll" "ul"
matrix inv_demand_single = J(30,4,.)
matrix colnames inv_demand_single = "price" "share_buy_all" "share_buy_f" "share_buy_f_1"
matrix inv_demand_VFC = J(30,3,.)
matrix colnames inv_demand_VFC = "price" "share_buy_VFC" "share_buy_other"

// note that the strata_1 conditioning is not needed since we only retain these households in the analysis data
forvalues price = 1/30 {
	loc p = `price'/10
	matrix inv_demand_all[`price',1]=`p'
	matrix inv_demand_ag_nut[`price',1]=`p'
	matrix inv_demand_diff_ag_nut[`price',1]=`p'
	matrix inv_demand_m_f[`price',1]=`p'
	matrix inv_demand_diff_m_f[`price',1]=`p'
	matrix inv_demand_m_f_R[`price',1]=`p'
	matrix inv_demand_diff_m_f_byR[`price',1]=`p'
	matrix inv_demand_ag_nut_R[`price',1]=`p'
	matrix inv_demand_diff_ag_nut_byR[`price',1]=`p'
	matrix inv_demand_diff_early[`price',1]=`p'
	matrix inv_demand_m_f_cell[`price',1]=`p'
	matrix inv_demand_diff_m_f_cell[`price',1]=`p'
	matrix inv_demand_VFC[`price',1]=`p'
	// Figure 2. Inverse demand for all
	reg buy_`price'
	matrix inv_demand_all[`price',2]=_b[_cons]
	// Figure 3a. by ag and ag nutrition script
	reg buy_`price' if inlist(nut,0)
	matrix inv_demand_ag_nut[`price',2]=_b[_cons]
	reg buy_`price' if inlist(nut,1)
	matrix inv_demand_ag_nut[`price',3]=_b[_cons]
	// Figure 4a. by gender
	reg buy_`price' if inlist(strata_1,1) & inlist(female,0)
	matrix inv_demand_m_f[`price',2]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(female,1)
	matrix inv_demand_m_f[`price',3]=_b[_cons]
	// differences and cis for inv-demand gradients	
	// Figure 3b. difference in inverse demand by ag nutrition script
	reg buy_`price' nut, vce(robust)
	matrix temp = r(table)
	matrix inv_demand_diff_ag_nut[`price',2]=_b[nut]
	matrix inv_demand_diff_ag_nut[`price',3]=temp[5,1]
	matrix inv_demand_diff_ag_nut[`price',4]=temp[6,1]
	// Figure 4b. difference in inverse demand by gender
	reg buy_`price' female if inlist(strata_1,1), vce(robust)
	matrix temp = r(table)
	matrix inv_demand_diff_m_f[`price',2]=_b[female]
	matrix inv_demand_diff_m_f[`price',3]=temp[5,1]
	matrix inv_demand_diff_m_f[`price',4]=temp[6,1]
	// Figure D3. Difference in inverse demand between early and late interviews
	reg buy_`price' interview_early, vce(robust)
	matrix temp = r(table)
	matrix inv_demand_diff_early[`price',2]=_b[interview_early]
	matrix inv_demand_diff_early[`price',3]=temp[5,1]
	matrix inv_demand_diff_early[`price',4]=temp[6,1]	
	// Figure 5a. Raw inverse demand for m and f in each region
	reg buy_`price' if inlist(strata_1,1) & inlist(female,0) & inlist(region_1,1), vce(robust)
	matrix inv_demand_m_f_R[`price',2]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(female,1) & inlist(region_1,1), vce(robust)
	matrix inv_demand_m_f_R[`price',3]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(female,0) & inlist(region_1,0), vce(robust)
	matrix inv_demand_m_f_R[`price',4]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(female,1) & inlist(region_1,0), vce(robust)
	matrix inv_demand_m_f_R[`price',5]=_b[_cons]
	// Figure 5b. Gender difference in demand by region
	reg buy_`price' female if inlist(strata_1,1) & inlist(region_1,1), vce(robust)
	matrix temp = r(table)
	matrix inv_demand_diff_m_f_byR[`price',2]=_b[female]
	matrix inv_demand_diff_m_f_byR[`price',3]=temp[5,1]
	matrix inv_demand_diff_m_f_byR[`price',4]=temp[6,1]		
	reg buy_`price' female if inlist(strata_1,1) & inlist(region_1,0), vce(robust)
	matrix temp = r(table)
	matrix inv_demand_diff_m_f_byR[`price',5]=_b[female]
	matrix inv_demand_diff_m_f_byR[`price',6]=temp[5,1]
	matrix inv_demand_diff_m_f_byR[`price',7]=temp[6,1]
	// Figure D1, Panel A. Raw inverse demand for m and f by mobile phone ownership
	reg buy_`price' if inlist(female,0) & inlist(resp_own_mobile,1), vce(robust)
	mat inv_demand_m_f_cell[`price',2]=_b[_cons]
	reg buy_`price' if inlist(female,1) & inlist(resp_own_mobile,1), vce(robust)
	mat inv_demand_m_f_cell[`price',3]=_b[_cons]
	reg buy_`price' if inlist(female,0) & inlist(resp_own_mobile,0), vce(robust)
	mat inv_demand_m_f_cell[`price',4]=_b[_cons]
	reg buy_`price' if inlist(female,1) & inlist(resp_own_mobile,0), vce(robust)
	mat inv_demand_m_f_cell[`price',5]=_b[_cons]
	// Figure D1, Panel B. M-F difference by mobile phone ownership
	reg buy_`price' female if inlist(resp_own_mobile,1), vce(robust)
	mat temp = r(table)
	mat inv_demand_diff_m_f_cell[`price',2]=_b[female]
	mat inv_demand_diff_m_f_cell[`price',3]=temp["ll","female"]
	mat inv_demand_diff_m_f_cell[`price',4]=temp["ul","female"]
	reg buy_`price' female if inlist(resp_own_mobile,0), vce(robust)
	mat temp = r(table)
	mat inv_demand_diff_m_f_cell[`price',5]=_b[female]
	mat inv_demand_diff_m_f_cell[`price',6]=temp["ll","female"]
	mat inv_demand_diff_m_f_cell[`price',7]=temp["ul","female"]
	// Figure 6a. Raw inverse demand by marketing script in each region
	reg buy_`price' if inlist(strata_1,1) & inlist(nut,0) & inlist(region_1,1), vce(robust)
	matrix inv_demand_ag_nut_R[`price',2]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(nut,1) & inlist(region_1,1), vce(robust)
	matrix inv_demand_ag_nut_R[`price',3]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(nut,0) & inlist(region_1,0), vce(robust)
	matrix inv_demand_ag_nut_R[`price',4]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(nut,1) & inlist(region_1,0), vce(robust)
	matrix inv_demand_ag_nut_R[`price',5]=_b[_cons]
	// Figures 6b, 6c. Difference in inverse demand separately by region
	reg buy_`price' nut if inlist(strata_1,1) & inlist(region_1,1), vce(robust)
	matrix temp = r(table)
	matrix inv_demand_diff_ag_nut_byR[`price',2]=_b[nut]
	matrix inv_demand_diff_ag_nut_byR[`price',3]=temp[5,1]
	matrix inv_demand_diff_ag_nut_byR[`price',4]=temp[6,1]		
	reg buy_`price' nut if inlist(strata_1,1) & inlist(region_1,0), vce(robust)
	matrix temp = r(table)
	matrix inv_demand_diff_ag_nut_byR[`price',5]=_b[nut]
	matrix inv_demand_diff_ag_nut_byR[`price',6]=temp[5,1]
	matrix inv_demand_diff_ag_nut_byR[`price',7]=temp[6,1]	
	// Figure D5. Inverse demand by whether VFC is the main network provider
	reg buy_`price' if inlist(strata_1,1) & inlist(WTP_main_phone_Vodafone,1), vce(robust)
	matrix inv_demand_VFC[`price',2]=_b[_cons]
	reg buy_`price' if inlist(strata_1,1) & inlist(WTP_main_phone_Vodafone,0), vce(robust)
	matrix inv_demand_VFC[`price',3]=_b[_cons]
}

// Figure 2. All inverse demand
preserve

clear
set obs 30

svmat inv_demand_all, names(col)

loc graph_title "figure2"

twoway connected ///
	share_buy price, ///
	msymbol(i...) ///
	lpattern(solid) ///
	lcolor(gs4) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(1)) graphregion(color(white)) ///
	plotregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'", replace)
graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace 
graph export "${figout}`graph_title'.png", as(png) replace 

restore

// Figure 3a. Graph inverse demand by script type
preserve

clear
set obs 30

svmat inv_demand_ag_nut, names(col)

loc graph_title "figure3a"

twoway connected ///
	share_buy_ag share_buy_nut price, ///
	msymbol(i...) ///
	lpattern(solid dash) ///
	lcolor(gs0 gs6) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(1) order(1 "Agriculture" 2 "Agriculture+Nutrition")) ///
	plotregion(color(white)) ///
	graphregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'.gph", replace)

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace


restore

// Figure 4a. Graph inverse demand by gender
preserve

clear
set obs 30

svmat inv_demand_m_f, names(col)

loc graph_title "figure4a"

twoway connected ///
	share_buy_m share_buy_f price, ///
	msymbol(i...) ///
	lpattern(solid dash) ///
	lcolor(gs0 gs6) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(1) order(1 "Males" 2 "Females")) ///
	plotregion(color(white)) ///
	graphregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'.gph", replace)

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace


restore

// graph differences and cis

// Figure 3b. Graph difference in inverse demand by script
preserve 

clear
set obs 30

svmat inv_demand_diff_ag_nut, names(col)

loc graph_title "figure3b"

twoway connected ///
	diff ll ul price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Agriculture+Nutrition vs. Agriculture" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace


restore

// Figure 4b. Graph difference in inverse demand by gender
preserve

clear
set obs 30

svmat inv_demand_diff_m_f, names(col)

loc graph_title "figure4b"

twoway connected ///
	diff ll ul price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Females vs. Males" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace


restore

// Figure 5a. plot raw inverse demand by gender and region

preserve

clear
set obs 30

svmat inv_demand_m_f_R, names(col)

loc graph_title "figure5a"

twoway connected ///
	dem_M_C dem_F_C dem_M_UW dem_F_UW price, ///
	msymbol(i...) ///
	lpattern(dash solid dash solid) ///
	lcolor(gs0 gs0 gs6 gs6) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(2) order(1 "Central Region Males" 2 "Central Region Females" 3 "UW Region Males" 4 "UW Region Females")) ///
	plotregion(color(white)) ///
	graphregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'.gph", replace)

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

// Figure 5b. Graph difference male and female inverse demand in the central region

preserve

clear
set obs 30

svmat inv_demand_diff_m_f_byR, names(col)

loc graph_title "figure5b"

twoway connected ///
	diff_cent ll_cent ul_cent price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Females vs. Males (Central region)" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

// Figure 5c. Graph difference male and female inverse demand in the upper west region
loc graph_title "figure5c"

twoway connected ///
	diff_uw ll_uw ul_uw price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Females vs. Males (Upper West region)" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

// Figure 6a. plot raw inverse demands by script and region

preserve

clear
set obs 30

svmat inv_demand_ag_nut_R, names(col)

loc graph_title "figure6a"

twoway connected ///
	dem_A_C dem_N_C dem_A_UW dem_N_UW price, ///
	msymbol(i...) ///
	lpattern(dash solid dash solid) ///
	lcolor(gs0 gs0 gs6 gs6) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(2) order(1 "Central Agriculture" 2 "Central Agriculture+Nutrition" 3 "UW Agriculture" 4 "UW Agriculture+Nutrition")) ///
	plotregion(color(white)) ///
	graphregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'.gph", replace)

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

// Figure 6b. Graph difference by script in central region

preserve

clear
set obs 30

svmat inv_demand_diff_ag_nut_byR, names(col)

loc graph_title "figure6b"

twoway connected ///
	diff_cent ll_cent ul_cent price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Agriculture+Nutrition vs. Agriculture" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

// Figure 6c. Graph difference by script in upper west region

loc graph_title "figure6c"

twoway connected ///
	diff_uw ll_uw ul_uw price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Agriculture+Nutrition vs. Agriculture" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

// Figures in appendix

// Figure D3. Graph difference in inverse demand for early and late interviews

preserve

clear
set obs 30

svmat inv_demand_diff_early, names(col)

loc graph_title "figureD3"

twoway connected ///
	diff ll ul price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Early Interviews vs. Late Interviews" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace


restore

/***************************************************************
// Figure D4. Ranges of inverse demand estimates by enumerator
***************************************************************/

matrix inv_dem_by_enum = J(1200,4,.)
matrix colnames inv_dem_by_enum = "price" "enumerator" "share_buy" "share_buy_overall"

loc i = 1
forvalues en = 1/40 {
	forvalues price = 1/30 {
		loc p = `price'/10
		matrix inv_dem_by_enum[`i',1]=`p'
		matrix inv_dem_by_enum[`i',2]=`en'
		reg buy_`price' if inlist(strata_1,1) & inlist(enumerator_id,`en')
		matrix inv_dem_by_enum[`i',3]=_b[_cons]
		reg buy_`price' if inlist(strata_1,1)
		matrix inv_dem_by_enum[`i',4]=_b[_cons]
		// add one to row index
		loc i = `i'+1
	}
}

preserve

clear

set obs 1200

svmat inv_dem_by_enum, names(col)

// identify extreme values

gsort +price +share_buy

bys price: gen min_share_buy = share_buy[1]

// generate 90% CI lower bound

bys price: gen ll90_share_buy = share_buy[3]

gsort +price -share_buy

bys price: gen max_share_buy = share_buy[1]

// generate 90% CI upper bound

bys price: gen ul90_share_buy = share_buy[3]

bys price: gen first_obs = 1 if _n == 1

keep if first_obs == 1

// Figure D4 Panel A Min-max bounds on inverse demand by enumerator
loc graph_title "figureD4a"

twoway connected ///
	share_buy_overall min_share_buy max_share_buy price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GHC)") ///
	legend(rows(1) order(1 "Mean share purchasing" 2 "Min/Max share purchasing")) ///
	graphregion(color(white)) plotregion(color(white)) ///
	saving("${figout}`graph_title'", replace)
graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace 
graph export "${figout}`graph_title'.png", as(png) replace 

// Figure D4 Panel B 90% ci inverse demand by enumerator
loc graph_title "figureD4b"

twoway connected ///
	share_buy_overall ll90_share_buy ul90_share_buy price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GHC)") ///
	legend(rows(1) order(1 "Mean share purchasing" 2 "90% share purchasing")) ///
	graphregion(color(white)) plotregion(color(white)) ///
	saving("${figout}`graph_title'", replace)
graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace 
graph export "${figout}`graph_title'.png", as(png) replace 

restore

// Figure D1 Panel A Inverse demand by mobile phone ownership and gender

preserve

clear
set obs 30

svmat inv_demand_m_f_cell, names(col)

loc graph_title "figureD1a"

twoway connected ///
	dem_M_M dem_F_M dem_M_NM dem_F_NM price, ///
	msymbol(i...) ///
	lpattern(dash solid dash solid) ///
	lcolor(gs0 gs0 gs6 gs6) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(2) order(1 "Males with mobile" 2 "Females with mobile" 3 "Males no mobile" 4 "Females no mobile")) ///
	plotregion(color(white)) ///
	graphregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'.gph", replace)

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

// Figure D1 Panel B Gender differences in inverse demand by mobile phone ownership

preserve

clear
set obs 30

svmat inv_demand_diff_m_f_cell, names(col)

// Mobile phone owners

loc graph_title "figureD1bleft"

twoway connected ///
	diff_cell ll_cell ul_cell price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Females vs. Males (with mobile phone)" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

// Non-mobile phone owners

loc graph_title "figureD1bright"

twoway connected ///
	diff_ncell ll_ncell ul_ncell price, ///
	msymbol(i...) ///
	lpattern(solid longdash =) ///
	lcolor(gs4 = =) ///
	lwidth(medium thin =) ///
	ytitle("Difference in share purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	saving("${figout}`graph_title'.gph", replace) ///
	legend(rows(1) order(1 "Females vs. Males (No mobile phone)" 2 "95% CI")) ///
	graphregion(color(white)) plotregion(color(white))

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

// Figure D5. plot inverse demand curves by VFC as main MNO

preserve

clear
set obs 30

svmat inv_demand_VFC, names(col)

// graph inverse demand curves: "price" "share_buy_VFC" "share_buy_other"

loc graph_title "figureD5"

twoway connected ///
	share_buy_VFC share_buy_other price, ///
	msymbol(i...) ///
	lpattern(dash solid) ///
	lcolor(gs0 gs6) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(2) order(1 "Vodafone mobile" 2 "non-Vodafone mobile")) ///
	plotregion(color(white)) ///
	graphregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'.gph", replace)

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

/************************************************************
// Figure D2. Inverse Demand by Gender and Household Type
************************************************************/

// call data that retain female only households
use `all_strata', clear


forvalues price = 1/30 {
	loc p = `price'/10
	matrix inv_demand_single[`price',1]=`p'
	// overall demand
	reg buy_`price' if inlist(strata_1,1) & inlist(female,0)
	matrix inv_demand_single[`price',2]=_b[_cons]
	// females in two-adult households
	reg buy_`price' if inlist(strata_1,1) & inlist(female,1)
	matrix inv_demand_single[`price',3]=_b[_cons]
	// females in female only households
	reg buy_`price' if inlist(strata_1,0) & inlist(female,1)
	matrix inv_demand_single[`price',4]=_b[_cons]
}

preserve
clear
set obs 30

svmat inv_demand_single, names(col)

loc graph_title "figureD2"

twoway connected ///
	share_buy_all share_buy_f share_buy_f_1 price, ///
	msymbol(i...) ///
	lpattern(dash solid longdash) ///
	lcolor(gs0 gs0 gs0) ///
	lwidth(medium...) ///
	ytitle("Share of households purchasing") ///
		ylabel(, grid angle(horizontal) format(%04.2f)) ///
	xtitle("Price (GhC)") ///
	legend(rows(2) order(1 "Males in 2-adult HH" 2 "Females in 2-adult HH" 3 "Female only HH")) ///
	plotregion(color(white)) ///
	graphregion(color(white)) ///
	xline(0.5, lpattern(dash) lcolor(gs4)) ///
	xla(0 "0" 0.5 "0.5" 1 "1" 2 "2" 3 "3", noticks) ///
	saving("${figout}`graph_title'.gph", replace)

graph export "${figout}`graph_title'.pdf", as(pdf) replace fontface(times)
graph export "${figout}`graph_title'.eps", as(eps) replace fontface(times)
graph export "${figout}`graph_title'.ps", as(ps) replace
graph export "${figout}`graph_title'.png", as(png) replace

restore

/****************************************************
* END OF WTP ANALYSIS FILE 2 CODE
****************************************************/

// close log file

log close `name_do_short'